﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using ProjectCommon2.Wcf.Behaviors;
using System.ServiceModel;
using System.ServiceModel.Channels;

namespace PolePosition.v3.ServiceProxy
{
    public class AdministrationService
    {
        #region Static Reference to the AdministrationService

        private static string m_ServiceEndpointFileName = "Administration.svc";

        internal static AdministrationServiceReference.AdministrationClient GetServiceClient()
        {
            Binding _Binding;
            EndpointAddress _EndPointAddress;

            _Binding = Setup.Binding;
            _EndPointAddress = Setup.GetEndPointAddress(m_ServiceEndpointFileName);

            AdministrationServiceReference.AdministrationClient _ServiceClient;
            _ServiceClient = new AdministrationServiceReference.AdministrationClient(_Binding, _EndPointAddress);

            _ServiceClient.Endpoint.Behaviors.Add(new AddUserCreatedModifiedClientHeaderBehavior());
            _ServiceClient.Endpoint.Behaviors.Add(new PushContextClientBehavior());

            // Update the data contract serializer for the operations to avoid max limit of objects in graph issue
            MaxDataContractSerializerOperationBehavior.ReplaceDataContactSerializer(_ServiceClient.Endpoint.Contract.Operations);

            //insert additional logic here?
            return _ServiceClient;
        }

        #endregion

        #region Public Service Methods

        public static EntityLibrary.LoginEntity DoLogin(string userName, string password)
        {
            EntityLibrary.LoginEntity _Return = null;
            AdministrationServiceReference.AdministrationClient _Client = GetServiceClient();
            try
            {
                _Return = _Client.DoLogin(userName, password);
                _Client.Close();
            }
            catch (Exception _Exc)
            {
                _Client.Abort();
                ProjectCommon2.Helpers.ExceptionHelper.Throw(_Exc);
            }

            return _Return;
        }

        public static EntityLibrary.LookupTables GetLookups()
        {
            EntityLibrary.LookupTables _Return = null;
            AdministrationServiceReference.AdministrationClient _Client = GetServiceClient();
            try
            {
                _Return = _Client.GetLookups();
                
                _Client.Close();
            }
            catch (Exception _Exc)
            {
                _Client.Abort();
                ProjectCommon2.Helpers.ExceptionHelper.Throw(_Exc);
            }

            return _Return;
        }

        #endregion

    }
}
